<template>
	<view class="page">
		<!-- 页面头部内容开始 -->
		<vk-data-page-header
			:title="page.title"
			:show-back="page.showBack"
			@back="pageBack"
		></vk-data-page-header>
		<!-- 页面头部内容结束 -->
		<view class="page-body">
			<!-- 页面主体内容开始 -->
			<vk-data-form
				v-loading="page.loading"
				ref="form1"
				v-model="form1.data"
				:rules="form1.props.rules"
				:action="form1.props.action"
				:form-type="form1.props.formType"
				:columns='form1.props.columns'
				:loading.sync="form1.props.loading"
				:labelWidth="form1.props.labelWidth"
				:footer-show="false"
				size="small"
				@success="formSuccess"
			>
				<template v-slot:order="{ form, keyName }">
					<view style="line-height: 60px;color: #606266;font-size: 14px;">
						<view style="margin-left: 85px;">
							<view>
								<text>下单后，超过</text>
								<vk-data-input-number v-model="form1.data.order.auto_cancel" :precision="0" :min="1" :max="1440" width="100px" placeholder="多少" style="margin: 0 10px;" size="small"></vk-data-input-number>
								<text>分钟未付款，系统自动取消订单。</text>
							</view>
							<view>
								<text>付款后，预计</text>
								<vk-data-input-number v-model="form1.data.order.plan_send" :precision="0" :min="1" :max="1440" width="100px" placeholder="多少" style="margin: 0 10px;" size="small"></vk-data-input-number>
								<text>天发货。（仅用于提示用户，若为空或为0，则不显示）</text>
							</view>
							<view>
								<text>发货后，超过</text>
								<vk-data-input-number v-model="form1.data.order.auto_confirm_receive" :precision="0" :min="1" :max="1440" width="100px" placeholder="多少" style="margin: 0 10px;" size="small"></vk-data-input-number>
								<text>天，系统自动确认收货。</text>
							</view>
							<view>
								<text>收货后，超过</text>
								<vk-data-input-number v-model="form1.data.order.auto_complete" :precision="0" :min="1" :max="1440" width="100px" placeholder="多少" style="margin: 0 10px;" size="small"></vk-data-input-number>
								<text>天，系统将订单改为已完成。</text>
							</view>
							<view>
								关于商品扣库存方式：统一为下单后扣减库存。
							</view>
						</view>
						<view><text>已收货，订单详情页提示语</text><el-input v-model="form1.data.order.receive_tips" style="width: 500px;margin-left: 10px;" placeholder="请输入内容"></el-input></view>
						<view><text>已完成，订单详情页提示语</text><el-input v-model="form1.data.order.complete_tips" style="width: 500px;margin-left: 10px" placeholder="请输入内容"></el-input></view>
						<view><text>已取消，订单详情页提示语</text><el-input v-model="form1.data.order.cancel_tips" style="width: 500px;margin-left: 10px" placeholder="请输入内容"></el-input></view>
					</view>
				</template>

				<template v-slot:order_refund="{ form, keyName }">
					<view style="line-height: 60px;color: #606266;font-size: 14px;">
						<view style="margin-left: 85px;">
							<view style="font-weight: bold;">用户主动申请退款的订单状态条件。</view>
							<view>
								<text>订单的状态必须为</text>
								<vk-data-input-select
									v-model="form1.data.order_refund.status"
									:localdata='[
										{ value:0, label:"已付款且未发货" },
										{ value:1, label:"已付款且未确认收货" },
										{ value:2, label:"已付款且未评价" },
									]'
									width="180px"
									style="margin: 0 10px;"
									:clearable="false"
									placeholder="请选择"
								></vk-data-input-select>
								<text>时，用户才能申请退款。</text>
							</view>
							<view style="font-weight: bold;">以下是当订单【已付款且未发货】时，用户申请退款，系统自动审核通过的条件，需全部满足，不填则忽略此条件。如都不填，则代表必须人工审核</view>
							<view style="display: flex;align-items: center;">
								<text>条件1：订单付款金额在</text>
								<vk-data-input-money v-model="form1.data.order_refund.max_amount" :precision="2" width="140px" placeholder="多少" style="margin: 0 10px;" size="small" rightText="元"/>
								<text>内。</text>
							</view>
							<view style="display: flex;align-items: center;">
								<text>条件2：本月累计退款次数在</text>
								<vk-data-input-number v-model="form1.data.order_refund.max_month_count" :precision="0" width="140px" placeholder="多少" style="margin: 0 10px;" size="small" rightText="次"/>
								<text>内。</text>
							</view>
							<view style="display: flex;align-items: center;">
								<text style="margin-right: 10px;">条件3：订单中不包含以下任意商品</text>
								<vk-data-input-table-select
									v-model="form1.data.order_refund.nin_goods_ids"
									:action="goodsSelect.action"
									:form-data="goodsSelect.formData"
									:columns='goodsSelect.columns'
									:multiple="goodsSelect.multiple"
									:queryColumns="goodsSelect.queryColumns"
									placeholder="请选择"
								></vk-data-input-table-select>
							</view>
						</view>
					</view>
				</template>
			</vk-data-form>
			<!-- 页面主体内容结束 -->
		</view>
		<view class="page-footer">
			<!-- 页面底部内容开始 -->
			<el-button v-if="page.showBack" plain style="margin-right: 20px;width: 80px;" @click="pageBack" :loading="form1.props.loading">{{ page.cancelText }}</el-button>
			<el-button type="primary" style="width: 80px;" @click="submitForm" :loading="form1.props.loading">{{ page.submitText }}</el-button>
			<!-- 页面底部内容开始 -->
		</view>
		<view class="page-dialog">
			<!-- 页面弹窗内容开始 -->

			<!-- 页面弹窗内容开始 -->
		</view>
	</view>
</template>

<script>
	var that;													// 当前页面对象
	var vk = uni.vk;									// vk实例
	export default {
		data() {
			// 页面数据变量
			return {
				// init请求返回的数据
				data:{

				},
				page: {
					title: "交易设置",
					submitText: "设置",
					cancelText: "返回",
					showBack: false,
					backPage:"",
					loading:false
				},
				form1: {
					// 表单请求数据，此处可以设置默认值
					data: {
						order:{
							auto_cancel:"",
							plan_send:"",
							auto_confirm_receive:"",
							auto_complete:"",
							receive_tips:"",
							complete_tips:"",
							cancel_tips:"",
						},
						order_refund:{
							status: 0,
							max_amount: "",
							max_month_count:""
						},
						refund_address:{
							name:"",
							mobile:"",
							address:{},
							address_detail:"",
							remark:"",
						}
					},
					// 表单属性
					props: {
						// 表单请求地址
						action: "admin/business/sys/order.updateOrderSetting",
						// 表单字段显示规则
						columns: [
							{ key: "", title: "订单相关", type: "bar-title" },
							{ key: "order", title: "订单超时时间，单位分钟", type: "text", showLabel:false },
							{ key: "", title: "退款审核相关", type: "bar-title" },
							{ key: "order_refund", title: "", type: "text", showLabel:false },
							{ key: "", title: "退货相关", type: "bar-title" },
							{ key: "refund_address", title: "", type: "object", width:300,
								columns:[
									{ key: "name", title: "联系人姓名", type: "text", width:300 },
									{ key: "mobile", title: "联系人手机号", type: "text", width:300 },
									{ key:"address", title:"退货仓库地址", type:"address", width:300 },
									{ key: "address_detail", title: "退货详情地址", type: "text", width:500 },
									{
										key:"remark", title:"备注", type:"textarea", width:500,
										autosize:{ minRows:4, maxRows:10 },
										maxlength:200,
										showWordLimit:true,
									}
								]
							},
						],
						// 表单验证规则
						rules: {

						},
						labelWidth: "100px"
					}
				},
				goodsSelect:{
					action:"admin/business/sys/goods.getList",
					multiple:true,
					formData:{
						status:0
					},
					columns:[
					 { key:"goods_thumb", title:"商品图", type:"avatar", imageWidth:40 },
					 { key:"name", title:"商品名称", type:"text", width:160, align:"left" },
					 { key:"categoryList", title:"所属分类", type:"text", width:140, defaultValue:'暂无',
						formatter: (val, row, column, index) => {
							let str = "";
							val.map((item, index) => {
								if (index > 0) str += "、";
								str += item.name;
							});
							return str;
						}
					 },
					 { key:"sku_list", title:"价格区间", type:"text", width:200, defaultValue:'暂无' ,
						formatter:(val, row, column, index) => {
							let a = [];
							val.map((item, index) => {
								a.push(Number(item.price))
							});
							let max = Math.max(...a);
							let min = Math.min(...a);
							if(max != min){
								// 如果最大值和最小值不相等
								return	vk.pubfn.priceFilter(min) +' - '+ vk.pubfn.priceFilter(max);
							}else{
								// 如果最大值和最小值相等
								return  vk.pubfn.priceFilter(min);
							}
						}
					 },
					 { key: "stock", title: "商品库存", type: "number", width: 110, show:["row"]  },
					],
					queryColumns:[
						{ key: "name", type: "text", title:"商品名称", placeholder:"请输入商品名称", width:220, mode:"%%" },
						{
							key:"category_ids", title:"商品分类", type:"cascader", width:250, mode:"in",
							action:"admin/business/sys/goodsCategory.kh_getList",
							props:{
								list:"rows",
								value:"_id",
								label:"name",
								children:"children",
								checkStrictly:true,
								emitPath:false
								// multiple:true
							}
						},
						{ key: "_id", title: "角色标识", type: "text", width: 150, mode: "%%", show:false }
					]
				}
			}
		},
		// 监听 - 页面每次【加载时】执行(如：前进)
		onLoad(options = {}) {
			that = this;
			vk = that.vk;
			that.options = options;
			that.init(options);
		},
		// 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快，会在页面进入动画完成前触发
		onReady(){

		},
		// 监听 - 页面每次【显示时】执行(如：前进和返回) (页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面)
		onShow() {

		},
		// 监听 - 页面每次【隐藏时】执行(如：返回)
		onHide() {

		},
		// 函数
		methods: {
			// 页面数据初始化函数
			init(options){
				vk.callFunction({
					url: 'client/pub/getMchInfo',
					loading: { name:"page.loading", that },
					data: {

					},
					success: (data) => {
						vk.setVuex('$user.mchInfo',data.mchInfo);
						vk.pubfn.objectAssign(that.form1.data.order, data.mchInfo.order);
						vk.pubfn.objectAssign(that.form1.data.order_refund, data.mchInfo.order_refund);
						vk.pubfn.objectAssign(that.form1.data.refund_address, data.mchInfo.refund_address);
					}
				});
			},
			// 页面跳转
			pageTo(path){
				vk.navigateTo(path);
			},
			// 页面返回
			pageBack(){
				const pages = getCurrentPages();
				if (pages.length>1 && pages[0].route !== that.$page.route) {
					vk.navigateBack();
				} else if(that.page.backPage) {
					vk.navigateTo(that.page.backPage);
				}
			},
			// 表单重置
			resetForm() {
				that.$refs.form1.resetForm();
			},
			// 表单提交
			submitForm(){
				that.$refs.form1.submitForm();
			},
			// 表单提交成功后的回调
			formSuccess(res){

			}
		},
		// 监听器
		watch:{

		},
		// 计算属性
		computed:{

		}
	}
</script>
<style lang="scss" scoped>

</style>
